<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      class="mt-dialog"
      append-to-body
      width="80%"
      @close="close"
    >

      <div class="top">
        <el-form :inline="true" style="margin-top: 10px">
          <el-form-item label="选择网站" class="mt-form-item-left">
            <website_select websites="[]" size="small" :multiple="false" @change="function(v){website_id=v; handleSearch();}" />
            <!--            <el-select v-model="website_id" clearable filterable @change="handleSearch">-->
            <!--              <el-option v-for="(item,index) in websites" :key="index" :label="item.domain"-->
            <!--                         :value="item.id"></el-option>-->
            <!--            </el-select>-->
          </el-form-item>
          <el-form-item>
            <el-date-picker
              ref="picker"
              v-model="timerange"
              type="datetimerange"
              value-format="timestamp"
              :picker-options="pickerOptions"
              range-separator="至"
              :default-time="['0:00:00', '23:59:59']"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="getList"
            />
          </el-form-item>
          <el-form-item label="产品链接">
            <el-input v-model="product_url" clearable />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="kw"
              placeholder="输入 任意字符 进行筛选"
              class="input-with-select"
              clearable
              @keyup.native.enter="handleSearch"
            >
              <el-dropdown slot="append" split-button type="primary" @click="getList">
                搜索
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item @click.native.stop="hanldeGetTopList">热销产品</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <!--<el-button slot="append" icon="el-icon-search" @click="getList">搜索</el-button>-->
            </el-input>
          </el-form-item>
        </el-form>

      </div>
      <div v-loading="listLoading">
        <el-row>
          <el-row :gutter="40" style="background:#fff;padding:16px 16px 0;margin-bottom:1px;">
            <el-col :xs="12" :sm="12" :lg="12" class="card-panel-col">
              <div class="card-panel">
                总销售数:{{ total_orders }} 总销售额:${{ total_sales }}
              </div>
            </el-col>
            <el-col :xs="12" :sm="12" :lg="12" class="card-panel-col">
              <div class="card-panel" style="float: right">
                <el-button v-if="website_id" type="text" @click="feedproductVisual=true">修改Feed</el-button>
              </div>
            </el-col>
          </el-row>
        </el-row>
        <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
          <el-table
            v-loading.fullscreen.lock="listLoading"
            :data="data"
            border
            fit
            highlight-current-row
            style="width: 100%"
            @cell-mouse-enter="(m) => m.active=true"
            @cell-mouse-leave="(m) => m.active=false"
            @sort-change="handleSort"
          >
            <el-table-column align="center" label="产品" prop="ps_url" sortable="custom">
              <template slot-scope="scope">
                <a :href="scope.row.product_url" target="_blank"><img
                  :src="scope.row.image"
                  style="max-width: 100px; max-height: 100px"
                ></a>
              </template>
            </el-table-column>
            <el-table-column align="center" label="产品ID" prop="product_id" sortable="custom" width="80">
              <template slot-scope="scope">
                <span @click="kw=scope.row.product_id;getList();">{{ scope.row.product_id }}</span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="产品链接" prop="ps_url" sortable="custom">
              <template slot-scope="scope">
                <a :href="scope.row.ps_url.indexOf('ttp') ? scope.row.ps_url: scope.row.psorder__website__domain + scope.row.ps_url" target="_blank">{{ scope.row.psorder_id }}</a>
              </template>
            </el-table-column>
            <el-table-column align="center" label="产品名称" prop="product_name" sortable="custom" />
            <el-table-column align="center" label="价格" prop="product_price" sortable="custom" width="80" />
            <el-table-column align="center" label="采购" prop="cost" sortable="custom" width="80" />
            <el-table-column align="center" label="毛利" prop="cost" sortable="custom" width="80">
              <template slot-scope="scope">
                {{ ((scope.row.product_price*scope.row.product_quantity) - scope.row.cost).toFixed(1) }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="数量" prop="product_quantity" sortable="custom" width="80" />
            <el-table-column align="center" label="时间" prop="create_at" sortable="120">
              <template slot-scope="scope">
                {{ scope.row.create_at.split('T')[0] }}<br>{{ scope.row.create_at.split('T')[1] }}
              </template>
            </el-table-column>
            <!--<el-table-column align="center" label="label2" prop="label_localization" sortable="custom">-->
            <!--<template slot-scope="scope">-->
            <!--<el-button type="text" @click="handleModify(scope.row)">{{scope.row.label_localization}} 设置</el-button>-->
            <!--</template>-->
            <!--</el-table-column>-->
          </el-table>

          <el-pagination
            v-if="data.length"
            background
            class="mt-pagination"
            :current-page="page"
            :page-sizes="[10,20,50,100]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handlePageChange"
          />
        </el-row>
      </div>
      <el-dialog
        v-loading="innerLoading"
        append-to-body
        title="修改信息"
        :width="width"
        :visible.sync="innerDialogVisible"
        :close-on-click-modal="false"
      >
        <el-form :model="temp" label-position="left" label-width="80px">
          <el-form-item label="选择网站" class="mt-form-item-left">
            <el-select v-model="temp.website_id" clearable filterable>
              <el-option
                v-for="(item,index) in websites"
                :key="index"
                :label="item.domain"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="产品ID">
            <el-input v-model="temp.id_product" />
          </el-form-item>
          <el-form-item label="label1">
            <el-input v-model="temp.label_1" />
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button v-if="temp.id_product !==''" type="primary" @click="GetLabe1">查 询</el-button>
          <el-button @click="innerDialogVisible = false">取 消</el-button>
          <el-button @click="ModifyDetail">修 改</el-button>
        </span>
      </el-dialog>
    </el-dialog>
    <feedproduct
      :pst-visible="feedproductVisual"
      :website_id="website_id"
      :product_id="kw"
      @closePstDialog="feedproductVisual=false"
    />
  </div>
</template>

<script>
import { GetProductSales, GetTopSales } from '@/api/orders/psorderdetail'
// import { Get as GetWebsite } from '@/api/erp/elicink/website'
import { SetPsLabel1, GetPsLabel1 } from '@/api/orders/ordersapi'
import Feedproduct from '@/views/orders/element/feedproduct'
import Website_select from '../../erp/elicink/elements/websites_select'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'Orderdetail',
  components: { Website_select, Feedproduct },
  props: ['orderStaticVisible', 'product_url', 'product_id', 'website_id', 'time_start', 'time_end'],
  data() {
    return {
      feedproductVisual: false,

      width: '80%',
      innerLoading: false,
      innerDialogVisible: false,
      temp: {
        website_id: '',
        id_product: '',
        label_1: ''
      },
      pagesize: 10,
      page: 1,
      total: 0,
      kw: '',
      data: [],
      websites: [],
      title: '产品销售情况',
      total_sales: 0,
      total_orders: 0,
      order_sales: 0,
      daily_sales: 0,
      dialogVisible: false,
      listLoading: false,
      lineChartData: {},
      timerange: '',
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime())
            start.setHours(0, 0, 0)
            picker.$emit('pick', [start, end])
          }
        },
        {
          text: '昨天下班后',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24)
            start.setHours(19, 0, 0)
            end.setHours(23, 59, 59)
            picker.$emit('pick', [start, end])
          }
        },
        {
          text: '昨天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 1)
            start.setHours(0, 0, 0)
            end.setTime(end.getTime() - 3600 * 1000 * 24 * 1)
            end.setHours(23, 59, 59)
            picker.$emit('pick', [start, end])
          }
        },
        {
          text: '最近3天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 3)
            start.setHours(0, 0, 0)
            picker.$emit('pick', [start, end])
          }
        },
        {
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            start.setHours(0, 0, 0)
            end.setTime(end.getTime() - 3600 * 1000 * 24)
            end.setHours(23, 59, 59)
            picker.$emit('pick', [start, end])
          }
        },
        {
          text: '过去14天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 14)
            start.setHours(0, 0, 0)
            end.setTime(end.getTime() - 3600 * 1000 * 24)
            end.setHours(23, 59, 59)
            picker.$emit('pick', [start, end])
          }
        },
        {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            start.setHours(0, 0, 0)
            end.setHours(23, 59, 59)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            start.setHours(0, 0, 0)
            end.setHours(23, 59, 59)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近6个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 180)
            start.setHours(0, 0, 0)
            end.setHours(23, 59, 59)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近1年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 365)
            start.setHours(0, 0, 0)
            end.setHours(23, 59, 59)
            picker.$emit('pick', [start, end])
          }
        }
        ]
      }
    }
  },
  watch: {
    product_id(v) {
      this.kw = v
    },
    website_id(v) {
      if (v) {
        this.temp.website_id = v
      }
    },
    time_start(v) {
      if (v) {
        if (this.timerange === '') {
          this.timerange = ['', '']
        }
        this.timerange[0] = new Date(v)
      }
    },
    time_end(v) {
      if (v) {
        if (this.timerange === '') {
          this.timerange = ['', '']
        }
        this.timerange[1] = new Date(v)
      }
    },
    orderStaticVisible(v) {
      this.dialogVisible = v
      if (v) {
        this.getList()
      }
    }
  },
  methods: {
    // handleGetWebsite() {
    //   GetWebsite().then(response => {
    //     this.websites = response.data.data;
    //   })
    // },
    handleModify(row) {
      this.innerDialogVisible = true
    },
    GetLabe1() {
      this.innerLoading = true
      GetPsLabel1(this.temp).then((response) => {
        this.temp.label_1 = response.data
        this.innerLoading = false
      }).catch(error => {
        console.log(error)
        this.innerLoading = false
      })
    },
    ModifyDetail() {
      this.innerLoading = true
      SetPsLabel1(this.temp).then((response) => {
        this.$notify({
          title: '修改成功',
          type: 'success'
        })
        this.innerLoading = false
      }).catch(error => {
        console.log(error)
        this.innerLoading = false
      })
    },
    getList() {
      this.listLoading = true
      if (this.timerange.length > 1) {
        if (this.timerange[0] instanceof Date) {
          this.timerange[0] = this.timerange[0].getTime()
        }
        if (this.timerange[1] instanceof Date) {
          this.timerange[1] = this.timerange[1].getTime()
        }
      }
      GetProductSales({
        website_id: this.website_id,
        start: this.timerange ? this.timerange[0] : '',
        end: this.timerange ? this.timerange[1] : '',
        product_url: this.product_url,
        kw: this.kw
      })
        .then(response => {
          this.scrollTop()
          this.data = response.data.data
          this.total_sales = response.data.total_sales
          this.total_orders = response.data.total_orders
          this.total = response.data.total
          // this.daily_sales = response.data.daily_sales
          // this.total_orders = response.data.total_orders
          // this.order_sales = response.data.order_sales
          // this.title = response.data.title
          this.listLoading = false
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    hanldeGetTopList() {
      this.listLoading = true
      if (this.timerange) {
        if (this.timerange[0] instanceof Date) {
          this.timerange[0] = this.timerange[0].getTime()
        }
        if (this.timerange[1] instanceof Date) {
          this.timerange[1] = this.timerange[1].getTime()
        }
      }

      GetTopSales({
        website_id: this.website_id,
        start: this.timerange ? this.timerange[0] : '',
        end: this.timerange ? this.timerange[1] : ''
      })
        .then(response => {
          this.scrollTop()
          this.data = response.data.data
          this.total_sales = response.data.total_sales
          this.total_orders = response.data.total_orders
          this.total = response.data.total
          this.listLoading = false
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    },
    // 点击搜索
    handleSearch() {
      this.page = 1
      this.temp.website_id = this.website_id
      this.getList()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    },
    close() {
      this.$emit('closeMyDialog', false)
    }
  }

}
</script>

<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .top >>> .el-input {
    width: auto;
  }

  .top >>> .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .top {
    padding-left: 20px;
    margin-bottom: 5px;
    margin-top: 28px;
  }

  .input-with-select {
    width: calc(100% - 200px);
  }

  .add-btn {
    float: right;
    margin-right: 20px;
    width: 150px
  }

  .mt-pagination {
    float: right;
    margin-top: 5px;
  }

  .app-container >>> .el-dialog {
    margin-top: 48px !important;
    margin-bottom: 5px;
  }

  .mt-dialog >>> .el-transfer-panel {
    width: calc(50% - 60px);
  }

  .mt-form >>> .el-select {
    width: 100%;
  }

  .mt-form >>> .el-input__inner {
    width: 100%;
  }

  .mt-dialog >>> .el-dialog {
    margin: 10px auto !important;
  }

  .mt-dialog >>> .el-dialog__body {
    padding-bottom: 60px;
    padding-top: 5px;
  }

  .top >>> .el-select {
    width: 300px;
  }

  .mt-form >>> a:visited {
    color: purple;
  }

  .mt-form >>> a:link {
    color: #409EFF;
  }

  .mt-form >>> a:active {
    color: red;
  }

  .mt-form >>> a:hover {
    color: red;
    text-decoration: underline;
  }
</style>
